﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace WebCrawlerTools
{
    public class STemplate
    {
 //       private int MAXSIZE = 128;
        private char[] Tag = { '$' };
        private ArrayList interList;
        private int currentIndex;

        private STemplate()
        {
            interList = new ArrayList();
            currentIndex = -1;
        }

        public STemplate(String template)
        {
            interList = new ArrayList();
            currentIndex = -1;
            initInterList(template);
        }

        internal SNode SNode
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }
    
        private void initInterList(String template)
        {
            string[] tag = template.Split(Tag);
            bool keyFlg = true;
            foreach (String t in tag)
            {
                if (keyFlg)
                {
                    SKey key = new SKey(t);
                    interList.Add(key);
                }
                else
                {
                    SVal val = new SVal(t);
                    interList.Add(val);
                }
                keyFlg = !keyFlg;
            }
        }

        public void Reset()
        {
            currentIndex = -1;
            foreach (SNode sn in interList)
            {
                sn.reset();
            }
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (Object o in interList)
            {
                sb.Append(o.ToString());
                sb.Append("\n");
            }
            return sb.ToString();
        }

        public SLine getValues()
        {
            SLine sl = new SLine();            
            foreach (SNode sn in interList)
            {
                if (sn.IsValue())
                {
                    SVal sv = (SVal)sn;
                    if (sv.Content != SNode.DUMMY_TAG)
                    {
                        sl.Data.Add(SFilter.RemoveHtmlTag(sv.Value));
                    }
                }
            }
            return sl;
        }
        public SLine getTitle()
        {
            SLine sl = new SLine();
            foreach (SNode sn in interList)
            {
                if (sn.IsValue())
                {
                    SVal sv = (SVal)sn;
                    if (sv.Content != SNode.DUMMY_TAG)
                    {                        
                        sl.Data.Add(sv.Content);
                    }
                }
            }
            return sl;
        }
        internal SNode getNextNode()
        {
            if (++currentIndex < interList.Count)
            {
                return (SNode)interList[currentIndex];
            }
            return null;
        }
    }
}
